<?php
/**
 * admin food category control
 */
class food_category extends CI_Controller {
	/**
	 * load common models and save them to $this without model suffix
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->model('food_category_model', 'food_category');
		$this->load->model('food_model', 'food');
        $this->load->library('session');
	}
	
	/**
	 * load first page food categories and display them, 
	 * and prepare for adding new food category
	 */
	function index()
	{
        $message = $this->session->flashdata('message');
		$food_category = $this->food_category->get_category();
		$data = array(
					'page_title'      => 'food category',
					'page_css'        => array('admin.css'),
					'page_js'         => array(),
					'food_category'  => $food_category,
                    'message'        => $message
					);
		$this->load->view('admin/food_category', $data);
	}
	//-------------------------------------------------------------------------
	/**
	 * add food category
	 */
	function add()
	{
        $en_name = $this->input->post('en_name');
        $ch_name = $this->input->post('ch_name');
        $row = $this->input->post('row_select');
        $col = $this->input->post('col_select');
        $result = $this->food_category->create($en_name, $ch_name, $row . "-" . $col);
        if(count($result['user'] ===0)){
            $this->session->set_flashdata('message', 'add successful');
        }
        else{
            $msg = "";
            foreach($result['user'] as $error){
                $msg .=  $error . "<br />";
            }
            $this->session->set_flashdata('message', $msg);
        }
        $this->load->helper('url');
        redirect("/admin/food_category/");
	}
	//--------------------------------------------------------------------------

    /**
     * read food category - ajax for nwo
     *
     * @internal param int $id
     */
	function read()
	{
		$id = $this->input->post('target_id');
        if($this->input->is_ajax_request()){
		    $category = $this->food_category->read($id, FALSE);
		    echo json_encode($category, JSON_HEX_TAG | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_HEX_APOS);
        }
	}
	/**
	 * update food category
	 */
	function update()
	{
        $id = $this->input->post('cat_id');
        $en_name = $this->input->post('en_name');
        $ch_name = $this->input->post('ch_name');
        $row = $this->input->post('row_select');
        $col = $this->input->post('col_select');
        //update it
        $result = $this->food_category->update($id, $en_name, $ch_name, $row . "-" . $col);
        if(count($result['user']) === 0){
            $this->session->set_flashdata('message', 'update successful');
        }
        else{
            $msg = "";
            foreach($result['user'] as $error){
                $msg .=  $error . "<br />";
            }
            $this->session->set_flashdata('message', $msg);
        }

        $this->load->helper('url');
        redirect("/admin/food_category/");
	}
	//--------------------------------------------------------------------------
	
	/**
	 * delete food category
	 */
	function delete()
	{
		$sequence = $this->input->post('delete_sequence');
        $del_a = explode("_", $sequence, 2);
        $result = $this->food_category->delete($del_a[0]);
        if(count($result['user']) === 0){
            $this->session->set_flashdata('message', 'delete successful');
        }
        else{
            $msg = "";
            foreach($result['user'] as $error){
                $msg .=  $error . "<br />";
            }
            $this->session->set_flashdata('message', $msg);
        }
        $this->load->helper('url');
        redirect("/admin/food_category/");
	}

    /**
     * swap food category
     */
    function swap()
    {
        $from_sequence = $this->input->post('swap_from');
        $to_sequence = $this->input->post('swap_to');
        $from_a = explode("_", $from_sequence, 2);
        $to_a = explode("_", $to_sequence, 2);
        $result = $this->food_category->swap($from_a[0], $to_a[0]);
        if(count($result['user']) === 0){
            $this->session->set_flashdata('message', 'swap successful');
        }
        else{
            $msg = "";
            foreach($result['user'] as $error){
                $msg .=  $error . "<br />";
            }
            $this->session->set_flashdata('message', $msg);
        }
        $this->load->helper('url');
        redirect("/admin/food_category/");
    }
}

/* End of file food_category.php */
/* Location: ./system/application/controllers/admin/food_category.php */
